<?php

function initSeo($type,$moduleId,$catId=0,$page=0,$articleData=null){
    //设置网站风格
    if($moduleId){
        $moduleSetting=setting("",$moduleId);
        $siteSetting=siteSetting("");
        $seoSetting=seoSetting("");
        $moduleData=module($moduleId);

        if($type=="detail") {
            $catId = getVal($articleData,"categoryId");
        }
        $categoryData=($moduleId && $catId)?category('module',$moduleId,$catId):array();

        //设置网站标题
        if($type){
            $keywords="";
            $description="";

            if($type=="detail"){
                $title="[内容标题][分隔符][网站名称]";
                $keywords="[内容标题]";
                $description="[文章简介]";
            }else if($type=="list"){
                $title="[分类名称][分隔符][模块名称][分隔符][网站名称]";
            }else{
                $title="[模块名称][分隔符][网站名称]";
            }

            $siteTitle=isset($siteSetting["siteTitle"])?$siteSetting["siteTitle"]:""; //网站名称
            $seoTitle=isset($seoSetting["seoTitle"])?$seoSetting["seoTitle"]:""; //网站SEO标题
            $moduleName=isset($moduleData["name"])?$moduleData["name"]:""; //模块名称
            $separator=isset($seoSetting["seoTitleSeparator"])?$seoSetting["seoTitleSeparator"]:""; //分隔符
            $seoDescription=isset($seoSetting["seoDescription"])?$seoSetting["seoDescription"]:""; //网站SEO描述
            $seoKeyword=isset($seoSetting["seoKeyword"])?$seoSetting["seoKeyword"]:""; //网站SEO关键字

            $map=array();
            $map["[模块名称]"]=$moduleName;
            $map["[分隔符]"]=$separator;
            $map["[网站SEO标题]"]=$seoTitle;
            $map["[网站名称]"]=$siteTitle;
            $map["[网站SEO关键字]"]=$seoKeyword;
            $map["[网站SEO描述]"]=$seoDescription;
            $map["[页码]"]=$page;
            $map["[内容标题]"]=($articleData && isset($articleData["title"]))?$articleData["title"]:"";
            $map["[分类名称]"]=($categoryData && isset($categoryData["name"]))?$categoryData["name"]:"";
            $map["[分类SEO标题]"]=($categoryData && isset($categoryData["seo_title"]))?$categoryData["seo_title"]:"";
            $map["[分类SEO关键字]"]=($categoryData && isset($categoryData["seo_keywords"]))?$categoryData["seo_keywords"]:"";
            $map["[分类SEO描述]"]=($categoryData && isset($categoryData["seo_description"]))?$categoryData["seo_description"]:"";
            $map["[文章简介]"]=($articleData && isset($articleData["description"]))?$articleData["description"]:"";

            $search=array();
            $replace=array();

            foreach($map as $mkey=>$mval){
                $search[]=$mkey;
                $replace[]=$mval;
            }

            //初始化
            $title=str_replace($search,$replace,$title);
            $keywords=str_replace($search,$replace,$keywords);
            $description=str_replace($search,$replace,$description);

            if($type=="index"){
                if(isset($moduleSetting["moduleIndexTitle"]) && $moduleSetting["moduleIndexTitle"]){
                    $title=str_replace($search,$replace,$moduleSetting["moduleIndexTitle"]);
                }
                if(isset($moduleSetting["moduleIndexKeywords"]) && $moduleSetting["moduleIndexKeywords"]){
                    $keywords=str_replace($search,$replace,$moduleSetting["moduleIndexKeywords"]);
                }
                if(isset($moduleSetting["moduleIndexDescription"]) && $moduleSetting["moduleIndexDescription"]){
                    $description=str_replace($search,$replace,$moduleSetting["moduleIndexDescription"]);
                }
            }else if($type=="list"){
                if(isset($moduleSetting["moduleListTitle"]) && $moduleSetting["moduleListTitle"]){
                    $title=str_replace($search,$replace,$moduleSetting["moduleListTitle"]);
                }
                if(isset($moduleSetting["moduleListKeywords"]) && $moduleSetting["moduleListKeywords"]){
                    $keywords=str_replace($search,$replace,$moduleSetting["moduleListKeywords"]);
                }
                if(isset($moduleSetting["moduleListDescription"]) && $moduleSetting["moduleListDescription"]){
                    $description=str_replace($search,$replace,$moduleSetting["moduleListDescription"]);
                }
            }else if($type=="detail"){
                if(isset($moduleSetting["moduleDetailTitle"]) && $moduleSetting["moduleDetailTitle"]){
                    $title=str_replace($search,$replace,$moduleSetting["moduleDetailTitle"]);
                }
                if(isset($moduleSetting["moduleDetailKeywords"]) && $moduleSetting["moduleDetailKeywords"]){
                    $keywords=str_replace($search,$replace,$moduleSetting["moduleDetailKeywords"]);
                }
                if(isset($moduleSetting["moduleDetailDescription"]) && $moduleSetting["moduleDetailDescription"]){
                    $description=str_replace($search,$replace,$moduleSetting["moduleDetailDescription"]);
                }
            }
            config()->set("app.seo.title",$title);
            config()->set("app.seo.keywords",$keywords);
            config()->set("app.seo.description",$description);
        }
    }
}


